package com.wjy.icu.service;


import com.wjy.icu.common.CacheManage;
import com.wjy.icu.common.constant.Constants;
import com.wjy.icu.common.constant.UserConstants;
import com.wjy.icu.common.enums.UserStatus;
import com.wjy.icu.common.exception.BusinessException;
import com.wjy.icu.common.exception.user.UserBlockedException;
import com.wjy.icu.common.exception.user.UserDeleteException;
import com.wjy.icu.common.exception.user.UserNotExistsException;
import com.wjy.icu.common.exception.user.UserPasswordNotMatchException;
import com.wjy.icu.common.publish.PublishFactory;
import com.wjy.icu.common.utils.MessageUtils;
import com.wjy.icu.common.utils.PasswordUtil;
import com.wjy.icu.domain.Icuuser;
import com.wjy.icu.domain.vo.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Slf4j
public class SysLoginService {

  @Autowired
  IUserService userService;
  @Autowired
  CacheManage cacheManage;

    /**
     * 登录
     */
    public Icuuser login(String username, String password) {

        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username, password)) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null"));
            throw new UserNotExistsException();
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.password.not.match"));
            throw new UserPasswordNotMatchException();
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.password.not.match"));
            throw new UserPasswordNotMatchException();
        }
        // 查询用户信息
        Icuuser user = userService.selectPetUserByPhone(username);

        if (user == null) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists"));
            throw new UserNotExistsException();
        }

        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.blocked"));
            throw new UserBlockedException();
        }
        if (!PasswordUtil.matches(user, password)) {
            throw new UserPasswordNotMatchException();
        }
        PublishFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
        return user;
    }
    /**
     * 登录
     */
    @Transactional
    public Icuuser loginByCode(String username, String code) {

        if (StringUtils.isAnyBlank(username, code)) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null"));
            throw new UserNotExistsException();
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.code.not.match"));
            throw new UserPasswordNotMatchException();
        }
        // 查询用户信息
        Icuuser user = userService.selectPetUserByPhone(username);
        if (user == null) {
            //开启默认注册
            UserDto userDto = new UserDto();
            userDto.setTel(username);
            userDto.setCode(code);
            userDto.setPassward("123456");
            userDto.setSmsType(2);
            userDto.setUserName(username);
            user = userService.register(userDto);
        }
        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            PublishFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
                    MessageUtils.message("user.blocked"));
            throw new UserBlockedException();
        }
        PublishFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
        return user;
    }
    public void logout(String loginName) {
        PublishFactory.recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"));
    }
    public Icuuser getUser(Long userId) {
        return userService.selectPetUserById(userId);
    }

}